﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for SiteMapUpdater
/// </summary>
public class SiteMapUpdater
{
    public static string _ThreadName;
    public static string _ForumName;

	public SiteMapUpdater()
	{
        _ThreadName = null;
        _ForumName = null;
	}

    //fixing the third levels down from the root node...
    //return the current fixed node
    public static SiteMapNode updateSiteMap(Object sender, SiteMapResolveEventArgs e){
        if (SiteMap.CurrentNode == null)
            return null;
        SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
        SiteMapNode tempNode = currentNode;

        if(currentNode.Key.Contains("forums"))
            return currentNode;

        string forumID = _ForumName;
        if (currentNode.Key.Contains("threads"))
            if (null != forumID)
            {
                tempNode.ReadOnly = false;
                tempNode.Title = forumID;
                tempNode.Url = "/ForumWebSite/Threads.aspx?Topic=" + forumID;
                return currentNode;
            }
        
        string threadID = _ThreadName;
        if (currentNode.Key.Contains("posts"))
            if ((null != threadID) && (null != forumID))
            {
                tempNode.ReadOnly = false;
                tempNode.Title = threadID;
                tempNode.Url = "/ForumWebSite/Posts.aspx?Topic=" + forumID + "&Thread=" + threadID;
                if (null != (tempNode = tempNode.ParentNode))
                {
                    tempNode.ReadOnly = false;
                    tempNode.Title = forumID;
                    tempNode.Url = "/ForumWebSite/Threads.aspx?Topic=" + forumID;
                }
                return currentNode;
            }

        return currentNode;
    }



}